<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;


class NewsController extends CommonController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $db = \DB::table("news");
       $list = $db->paginate(1); //5条每页浏览
        
       return view("admin.news.index",['list'=>$list]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view("admin.news.create");
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //获取要添加的数据
        $data = $request->only("title",'src');
        //执行添加
        $id = \DB::table("news")->insertGetId($data);
        //判断
        if($id>0){
            $info = "新闻公告信息添加成功！";
        }else{
            $info = "新闻公告信息添加失败！";
        }
        
        return view("admin.news.info",['info'=>$info]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
		$list = \DB::table('news')->where('id',$id)->first();
        return view('admin.news.edit',['list'=>$list]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
		//获取信息
		$title = $request->input("title");
		$src = $request->input("src");	
	
		\DB::table("news")->where("id",$id)->update(['title'=>$title,'src'=>$src]);
		
		 //判断
        if($id>0){
            $info = "新闻公告信息修改成功！";
        }else{
            $info = "新闻公告信息修改失败！";
        }
        
        return view("admin.news.info",['info'=>$info]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
         \DB::table('news')->delete($id);
        return redirect("admin/news");
    }
}
